﻿using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using API;
using Foundation;
using mb;
using mb.iOS;
using Masonry;
using MyExtensions;
using UIKit;

namespace libmp
{
    public class VCBook : UIViewController
    {
        static ILog logger = mb.ServiceLocator.Instance.Get<ILog>();

        MyScrollView outScrollView;
        MyScrollView inScrollView;
        UIView topBar1;
        UIView topBar2;

        public VCBook()
        {
            TabBarItem = new UITabBarItem("阅读", null, 0);
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            View.BackgroundColor = UIColor.Purple;
            logger.Log("VCBook ViewDidLoad");

            var tv = new UILabel();
            tv.Text = "shouldOutScroll:";
            tv.BackgroundColor = UIColor.Yellow;
            tv.SetParent(View);
            tv.Frame = new CoreGraphics.CGRect(200, 50, 400, 80);

            int outMaxScrollHeight = 100;
            bool isFixing = false;
            EventHandler fixScroll = (obj, e) =>
            {
                var out_v1 = outScrollView.PanGestureRecognizer.VelocityInView(outScrollView).Y;
                var out_v2 = outScrollView.PanGestureRecognizer.VelocityInView(inScrollView).Y;
                var in_v1 = inScrollView.PanGestureRecognizer.VelocityInView(inScrollView).Y;
                var out_offy = outScrollView.ContentOffset.Y;
                var in_offy = inScrollView.ContentOffset.Y;

                if (isFixing)
                {
                    // logger.Log("!!!!!! ignore nest", out_offy, in_offy);
                    // return;
                }
                isFixing = true;

                int t = DateTime.Now.Millisecond;
                logger.Log("<<<--------------", t);
                logger.Log("speed out", out_v1, out_v2);
                logger.Log("speed in", in_v1);
                logger.Log("offset", out_offy, in_offy);

                if (out_v1 < 0)
                {
                    if (out_offy < outMaxScrollHeight && Math.Abs(out_offy - outMaxScrollHeight) > 1)
                    {
                        inScrollView.ContentOffset = new CoreGraphics.CGPoint(0, 0);
                        logger.Log("out_offy<0 reset in_off->0");

                    }

                    if (inScrollView.ContentOffset.Y > 0)
                    {
                        outScrollView.ContentOffset = new CoreGraphics.CGPoint(0, outMaxScrollHeight);
                        logger.Log("in_off>0 reset out_off");
                    }
                }
                else
                {
                    if (inScrollView.ContentOffset.Y > 0)
                    {
                        outScrollView.ContentOffset = new CoreGraphics.CGPoint(0, outMaxScrollHeight);
                    }

                }
                logger.Log(">>>--------------", t);
                isFixing = false;
            };

            Func<bool> isOutSrollFirst = () =>
            {

                return outScrollView.ContentOffset.Y <= 100;
            };

            outScrollView = new MyScrollView()
            {
                BackgroundColor = UIColor.Gray,
                    MyShouldRecognizeSimultaneously = (v, rg1, rg2) =>
                    {
                        var rg1Pan = rg1 as UIPanGestureRecognizer;

                        logger.Log("out check sim");
                        return true;
                    }
            };
            outScrollView.SetParent(View);
            outScrollView.MakeConstraints(m =>
            {
                m.Left.EqualTo(View);
                m.Width.EqualTo(View);
                m.Bottom.EqualTo(this.BottomLayoutGuideTop());
                m.Top.EqualTo(tv.Bottom());
            });
            outScrollView.Scrolled += fixScroll;

            var outPanGR = outScrollView.PanGestureRecognizer;
            AddRuleToScrollView(outScrollView, 30);

            topBar1 = new UIView() { BackgroundColor = UIColor.Red };
            topBar1.SetParent(outScrollView);
            topBar1.MakeConstraints(m =>
            {
                m.Width.EqualTo(outScrollView);
                m.Top.EqualTo(outScrollView);
                m.Left.EqualTo(NSObject.FromObject(100));
                m.Height.EqualTo(NSObject.FromObject(100));
            });

            topBar2 = new UIView() { BackgroundColor = UIColor.Green };
            topBar2.SetParent(outScrollView);
            topBar2.MakeConstraints(m =>
            {
                m.Width.EqualTo(outScrollView);
                m.Top.EqualTo(topBar1.Bottom());
                m.Left.EqualTo(NSObject.FromObject(100));
                m.Height.EqualTo(NSObject.FromObject(100));
            });

            inScrollView = new MyScrollView()
            {
                BackgroundColor = UIColor.Blue,
                    MyShouldRecognizeSimultaneously = (v, rg1, rg2) =>
                    {
                        bool ok = false;
                        var rg1Pan = rg1 as UIPanGestureRecognizer;
                        if (rg1Pan != null && rg2 == outPanGR)
                        {
                            ok = isOutSrollFirst();
                        }

                        // logger.Log("check sim", ok);
                        return true;
                    },
            };
            inScrollView.Scrolled += fixScroll;
            inScrollView.SetParent(outScrollView);
            inScrollView.MakeConstraints(m =>
            {
                m.Left.EqualTo(NSObject.FromObject(100));
                m.Top.EqualTo(topBar2.Bottom());
                // m.Right.EqualTo(outScrollView.Right());
                m.Bottom.EqualTo(View).Offset(-100);
                // m.Height.EqualTo(NSObject.FromObject(1000));
                m.Width.EqualTo(outScrollView);
            });
            AddRuleToScrollView(inScrollView, 25);
        }

        void AddRuleToScrollView(UIScrollView sv, int c)
        {
            int segH = 50;
            int xb = 50;
            int y = 0;
            for (int i = 0; i < c; i++)
            {
                y = i * segH;
                var tv = new UILabel();
                tv.Frame = new CoreGraphics.CGRect(0, y, xb, 30);
                tv.Text = y.ToString();
                tv.SetParent(sv);
            }
            sv.ContentSize = new CoreGraphics.CGSize(400, y);
        }

    }
}